package com.playball.lottery.frm.buyhall;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.playball.lottery.ApplicationData;
import com.playball.lottery.R;
import com.playball.lottery.customview.BallDTO;
import com.playball.lottery.customview.MyDialog;
import com.playball.lottery.customview.NewDelButtonAdapter;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.frm.yhzx.BetProtocolActivity;
import com.playball.lottery.frm.yhzx.JddRechargeListActivity;
import com.playball.lottery.frm.yhzx.LoginActivity;
import com.playball.lottery.frm.yhzx.MyHuiYuan;
import com.playball.lottery.frm.yhzx.MySetYongJin;
import com.playball.lottery.frm.yhzx.MyHuiYuan.MyHUIyuanListAdapter.ViewHolder;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.util.Constants;
import com.playball.lottery.util.ConvertJsonToList;
import com.playball.lottery.util.DefConstants;
import com.playball.lottery.util.DynArrayInt;
import com.playball.lottery.util.GetIDAsyncLoader;
import com.playball.lottery.util.LotConstants;
import com.playball.lottery.util.MyPreference;
import com.playball.lottery.util.PreferencesUtils;
import com.playball.lottery.util.SysApplication;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SixFuckBetActivity extends Activity/*ListActivity*/ implements DefConstants {
	Button buy_ssqxhlb_Button06;
	/** 发起合买 */
	Button buy_ssqxhlb_btn_hemai;
	TextView buy_ssqxhlb_TextView03;
	View buy_ssqxhlb_linearlayout01;
	View buy_ssqxhlb_linearlayout02;

	private DynArrayInt red_dintInt;
	private DynArrayInt blue_dintInt;
	private DynArrayInt money_dintInt;
	private int playTypeId = 0;
	private int lotID;
	private String issueID;
	private String issueName;
	private int buy_icon;
	private ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
	LinearLayout foot_layout;
	TextView tv_bet_protocol;
	CheckBox cb_bet_protocol;

	boolean[] ball_1_49;
	boolean[] ball1_t1_t17;
	boolean[] ball1_zm_t1_t4;
	boolean[] ball_zmt_zmt1_zmt17;
	boolean[] ball_tmsx_tmsx1_12;
	int sc_zt_play_index;//正N特
	private String[] dyn;

	private LinearLayout ceshi;
	
	TextView tv_six_zmt_type;

	private Context context;
	private PreferencesUtils pUtil;
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			Bundle b = msg.getData();
			issueID = b.getString("issueID");
			issueName = b.getString("issueName");
		}
	};
	private int allMoney;

	View view_six_zm;
	private String strPlayType;
	String pl = "";
	List<Map<String, Object>> plList;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_six_fuck_bet);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
						| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		SysApplication.getInstance().addActivity(this);
		context = this;
		pUtil = new PreferencesUtils(context, Constants.PREFERENCES_NAME);
		initViewChange();
	}

	public void onResume() {
		super.onResume();
		buy_ssqxhlb_Button06.setEnabled(true);
	}

	public void onPause() {
		super.onPause();
		
	}

	/**
	 * 初始化 判断是不是 幸运装盘过来的
	 */
	private void initViewChange() {
//		this.getListView().addFooterView(foot_layout);
		view_six_zm = findViewById(R.id.view_six_zm);
		allMoney = 0;
		// 获得传来的数组，并放入ListView
		Intent intent = getIntent();
		ball_1_49 = intent.getBooleanArrayExtra("bet_ball_1_49");
		ball1_t1_t17 = intent.getBooleanArrayExtra("bet_ball1_t1_t17");
		issueID = intent.getStringExtra("IssueID");// 可以不要
		issueName = intent.getStringExtra("IssueName");// -1 最新的
		lotID = intent.getIntExtra("LotID", LOT_ID_LHC);
		playTypeId = intent.getIntExtra("PlayTypeId", 0);
		strPlayType = intent.getStringExtra("strPlayType");
		pl = intent.getStringExtra("pl");
		plList = ConvertJsonToList.getList(pl);
		initView();
		tv_six_zmt_type = (TextView) findViewById(R.id.tv_six_zmt_type);
		LayoutInflater inflater = LayoutInflater.from(this);
		foot_layout = (LinearLayout) inflater.inflate(R.layout.bet_protocol_foot, null);
		tv_bet_protocol = (TextView) foot_layout.findViewById(R.id.bet_protocol);
		cb_bet_protocol = (CheckBox) foot_layout.findViewById(R.id.bet_protocol_choice);
//		tv_bet_protocol.setText(Html.fromHtml("<u>我已满18周岁并同意《服务条款》"));// "<u>我已阅读并同意《代购合买协议》"
		tv_bet_protocol.setVisibility(View.VISIBLE);//显示服务协议
		cb_bet_protocol.setChecked(true);
		tv_bet_protocol.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(SixFuckBetActivity.this, BetProtocolActivity.class), 0);
			}
		});
		tv_six_zmt_type.setVisibility(View.GONE);
		if(playTypeId == 9601){
			view_six_zm.setVisibility(View.VISIBLE);
			ball_1_49 = intent.getBooleanArrayExtra("bet_ball_1_49");
			ball1_t1_t17 = intent.getBooleanArrayExtra("bet_ball1_t1_t17");
		}else if(playTypeId == 9602){
			view_six_zm.setVisibility(View.VISIBLE);
			ball_1_49 = intent.getBooleanArrayExtra("bet_ball_1_49");
			ball1_zm_t1_t4 = intent.getBooleanArrayExtra("bet_ball1_zm_t1_t4");
		}else if(playTypeId == 9603){
			view_six_zm.setVisibility(View.VISIBLE);
			ball_1_49 = intent.getBooleanArrayExtra("bet_ball_1_49");
			ball_zmt_zmt1_zmt17 = intent.getBooleanArrayExtra("bet_ball_zmt_zmt1_zmt17");
			sc_zt_play_index = intent.getIntExtra("sc_zt_play_index", -1);
			tv_six_zmt_type.setVisibility(View.VISIBLE);
			tv_six_zmt_type.setText("正"+sc_zt_play_index+"特");
		}else if(playTypeId == 9604){
			view_six_zm.setVisibility(View.VISIBLE);
			ball_tmsx_tmsx1_12 = intent.getBooleanArrayExtra("bet_ball_tmsx_tmsx1_12");
		}
		
		if (issueID == null || issueName == null) {
			GetIDAsyncLoader task = new GetIDAsyncLoader(this, handler, lotID);
			String[] parms = new String[2];
			parms[0] = "200";
			JSONObject jsob = new JSONObject();
			try {
				// 设置请求所需参数
				jsob.put("LottID", lotID);
			} catch (JSONException ex) {
				Log.e("error", ex.toString());
			}
			parms[1] = jsob.toString();
			task.execute(parms);
		}

		// 购买
		buy_ssqxhlb_Button06.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				buy_ssqxhlb_Button06.setEnabled(false);
				if (ApplicationData.getInstance().isUserLogin()) {
					if (issueID == null || issueName == null) {
						Toast.makeText(context, R.string.the_network_is_not_to_force, Toast.LENGTH_SHORT).show();
						buy_ssqxhlb_Button06.setEnabled(true);
						return;
					}
					if (allMoney < 1) {
						Toast.makeText(SixFuckBetActivity.this, "请输入投注金额",
								Toast.LENGTH_SHORT).show();
						buy_ssqxhlb_Button06.setEnabled(true);
						return;
					} else {
						if (cb_bet_protocol.isChecked() == false) {
							Toast.makeText(SixFuckBetActivity.this, R.string.need_to_agree_to_just_can_continue_to_buy,
									Toast.LENGTH_SHORT).show();
							buy_ssqxhlb_Button06.setEnabled(true);
							return;
						}
							startBuyThread();
							buy_ssqxhlb_Button06.setEnabled(true);
					}
				} else {
					Intent intent = new Intent();
					intent.setClass(context, LoginActivity.class);
					startActivity(intent);
				}
			}
		});
		list = getData();
//		InitBaseAdapter(list);
		LinearLayout view_bet_layout = (LinearLayout) findViewById(R.id.view_bet_layout);
		
		LayoutInflater mInflater =  LayoutInflater.from(SixFuckBetActivity.this);
		allMoney = 0;
		for(int i=0;i<list.size();i++){
			View convertView = mInflater.inflate(R.layout.item_six_zm, null);
			if(playTypeId == 9601 || playTypeId == 9602 || playTypeId == 9603|| playTypeId == 9604){
				TextView tv_item_1 = (TextView) convertView.findViewById(R.id.tv_item_1);
				TextView tv_item_2 = (TextView) convertView.findViewById(R.id.tv_item_2);
				final EditText et_item = (EditText) convertView.findViewById(R.id.et_item);
				tv_item_1.setText(list.get(i).get("bet_name").toString());
				tv_item_2.setText(list.get(i).get("peilv").toString());
				if(Integer.parseInt(list.get(i).get("money").toString())>0){
					et_item.setText(""+Integer.parseInt(list.get(i).get("money").toString()));
					allMoney = allMoney + Integer.parseInt(list.get(i).get("money").toString());
				}else{
					et_item.setText(""+10);
					allMoney = allMoney + 10;
				}
				final int pos = i;
				et_item.addTextChangedListener(new TextWatcher() {
					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {
					}
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count, int after) {
					}
					private boolean isChanged = false;
					@Override
					public void afterTextChanged(Editable s) {
						if (isChanged) {
							return;
						}
						String str2 = s.toString().trim().replaceFirst("^0*", "");
						isChanged = true;
						et_item.setText(str2);
						et_item.setSelection(str2.length());
						list.get(pos).put("money", s.toString());
						allMoney = 0;
						for(int jjj=0;jjj<list.size();jjj++){
							if(list.get(jjj).get("money").toString().isEmpty() == false)
								allMoney = allMoney + Integer.parseInt(list.get(jjj).get("money").toString());
						}
						buy_ssqxhlb_TextView03.setText(""+allMoney);
						isChanged = false;
					}
				});
				
			}
			buy_ssqxhlb_TextView03.setText(""+allMoney);
			view_bet_layout.addView(convertView);
		}
		view_bet_layout.addView(foot_layout);
	}

	class AsyncLoader extends AsyncTask<String, Integer, String> {// 继承AsyncTask
		String action;
		ShowTipDialog showdialog = new ShowTipDialog();
		protected void onPreExecute() {
			showdialog.showDialog(SixFuckBetActivity.this);
		}
		@Override
		protected String doInBackground(String... paramsArr) {
			String strResult = null;
			action = paramsArr[1];
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(paramsArr[0], UserID, action, getApplicationContext());
			return strResult;
		}
		protected void onProgressUpdate(Integer... progress) {
		}
		protected void onPostExecute(String result) {
			buy_ssqxhlb_Button06.setEnabled(true);
			if (result != null) {
				if (action.equals("207")) {
					Buy(result);
				}
				showdialog.closeDialog();
			} else {
				Toast.makeText(SixFuckBetActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}
		protected void onCancelled() {
			buy_ssqxhlb_Button06.setEnabled(true);
		}

	}

	/**
	 * 拼接投注号码
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getLotNum() {
		List<BallDTO> arrayList = ApplicationData.getInstance().getList_ssqdto();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (BallDTO elvdto : arrayList) {
			String ssq_ball = elvdto.getSsq_ball();
			ssq_ball = ssq_ball.replace("-", " + ");
			ssq_ball = ssq_ball.substring(0, ssq_ball.lastIndexOf("+"));
			ssq_ball = ssq_ball.replace(",", " ");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("number", ssq_ball);
			if ((ssq_ball.split("\\+")[0]).length() > 18 || ((ssq_ball.split("\\+")[1]).length() > 4)) {
				map.put("playid", SSQ_ZX_F);// "07 11 12 15 26 30 33 + 05"或"07 11 12 15 26 30 + 05 08"
			} else {
				map.put("playid", SSQ_ZX_D);// 单式
			}
			list.add(map);
		}
		return list;
	}

	private void startBuyThread() {
		List<Map<String, Object>> numArr = new ArrayList<Map<String, Object>>();
//		numArr = getLotNum();
		String number = "";
		if(playTypeId == 9601 || playTypeId == 9602  || playTypeId == 9603 || playTypeId == 9604){
			for(int i=0;i<list.size();i++){
				if(list.get(i).get("money").toString().isEmpty() == false){
					if(Integer.parseInt(list.get(i).get("money").toString())>0){
						number = number + list.get(i).get("bet_num").toString()+"="+list.get(i).get("money").toString()  +",";
					}
				}
			}
			number = number.substring(0, number.length()-1);
			if(playTypeId == 9603){
				number = number + "|pos="+sc_zt_play_index;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("number", number);
			map.put("playid", playTypeId);
			numArr.add(map);
		}
		
		
		AsyncLoader task = new AsyncLoader();
		dyn = new String[2];
		JSONObject params = new JSONObject();
		try {
			JSONArray array1 = new JSONArray();
			for (int i = 0; i < numArr.size(); i++) {
				JSONObject o = new JSONObject(numArr.get(i));
				array1.put(o);
			}
			params.put("SchemeType", 1);// 方案类型 1自购，2合买，3保存 1
			params.put("BetType", 1);// 投注类型 1选号投注 2单式上传 3先发后传 1
			params.put("BonusScale", 0);
			params.put("AssureShare", 0);
			params.put("Description", "");
			params.put("OpenUserList", "");
			params.put("SecrecyLevel", 4);
			params.put("BuyShare", 1 * allMoney);
			params.put("Multiple", 1);
			params.put("LotteryID", lotID);
			params.put("IssueID", issueID); 	// 彩票期次Id
			params.put("IssueName", issueName);	// 彩票期次
			params.put("Number", array1);
			params.put("Money", 1 * allMoney);
		} catch (JSONException ex) {
			// TODO Auto-generated catch block
			Log.e("error", ex.toString());
		}
		dyn[0] = params.toString();
		dyn[1] = "207";// 200
		task.execute(dyn);
	}

	public void Buy(String strResult) {
		try {
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject js = (JSONObject) jsonParser.nextValue();
			if (Integer.parseInt(js.getString("Code")) < 0) {
				buy_icon = R.drawable.buy_failure;
				String balanceNotFull = js.getString("Data");
//				if(js.getString("Data").contains(Constants.BALANCE_BUZU)) {
//					Intent intent = new Intent();
//					intent.setClass(context, JddRechargeListActivity.class);
//					intent.putExtra("params", dyn[0]);
//					startActivityForResult(intent, REQ_CODE_NO_MONEY_2_RECHARGE);
//				} else {
					new ShowTipDialog().showBuyInfo(SixFuckBetActivity.this, balanceNotFull, "购买失败", buy_icon, dyn[0]);
//				}
			} else {
				buy_icon = R.drawable.buy_success;
				String msg = "";
				if ("-1".equals(issueName)) {
					msg = "发起六合彩" + " 购买方案\n\n我的认购：" + (allMoney * 1) 
							+ "元"+"\n方案编号："+js.getString("Data").toString();
				} else {
					msg = "发起六合彩" + issueName + "期购买方案\n\n我的认购：" 
				+ (allMoney * 1 ) + "元"+"\n方案编号："+js.getString("Data").toString();
				}
				if (!isFinishing()) {
					new ShowTipDialog().showBuyInfo(SixFuckBetActivity.this, msg, "投注成功", buy_icon, dyn[0]);
				}
				// 存贮数据
				MyPreference.getInstance(this).storeBLotID(LotConstants.LOT_ID_LHC);

			}
		} catch (JSONException ex) {
			// 异常处理代码
			Log.e("error", ex.toString());
		}
	}

	public String getBalance() {
		String money = "";
		String strResult = null;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			JSONObject params = new JSONObject();
			String action = "107";// 108获取用户基本信息
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(params.toString(), UserID, action,
					getApplicationContext());
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject js = (JSONObject) jsonParser.nextValue();
			map = ConvertJsonToList.getMap(js.getString("Data"));
			money = map.get("Balance").toString();
		} catch (Exception ex) {
			Log.e("error", ex.toString());
		}
		return money;
	}

	public void initView() {
		ceshi = (LinearLayout) findViewById(R.id.regist);
		buy_ssqxhlb_linearlayout01 = findViewById(R.id.buy_ssqxhlb_linearlayout01);
		buy_ssqxhlb_linearlayout02 = findViewById(R.id.buy_ssqxhlb_linearlayout02);
		buy_ssqxhlb_Button06 = (Button) findViewById(R.id.buy_ssqxhlb_Button06);
		buy_ssqxhlb_btn_hemai = (Button) findViewById(R.id.buy_ssqxhlb_btn_hemai);
		LinearLayout showOrHideLayout = (LinearLayout) findViewById(R.id.ll_show_hide);
		buy_ssqxhlb_TextView03 = (TextView) findViewById(R.id.buy_ssqxhlb_TextView03);
		titleBar();
		ceshi.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			@Override
			public void onGlobalLayout() {
				int heightDiff = ceshi.getRootView().getHeight() - ceshi.getHeight();
				// 判断焦点在哪里
			}
		});
	}

//	public void InitBaseAdapter(ArrayList list) {
//		sixFuckListAdapter listAdapter = new sixFuckListAdapter(list);
//		setListAdapter(listAdapter);
//	}

	float[] ball_tm_t1_t17_pv = {1.90f,1.90f,1.90f,1.90f,1.90f,1.90f,2.70f,2.90f,2.90f,1.90f,1.90f,3.75f,3.75f,3.75f,3.75f,1.90f,1.90f};
	String[] ball_tm_t1_t17_na = {"特单","特双","特大","特小","合单","合双","红波","绿波","蓝波","尾大","尾小","大单","小单","大双","小双","家禽","野兽"};
	String[] ball_zm_t1_t4_na = {"和大","和小","和单","合双"};
	String[] ball_zmt_zmt1_zmt17_na = {"单","双","大","小","红波","蓝波","绿波","合大","合小","合单","合双","尾大","尾小"};
	float[] ball_zmt_zmt1_zmt17_pv = {1.90f,1.90f,1.90f,1.90f,2.75f,2.90f,2.90f,1.90f,1.90f,1.90f,1.90f,1.90f,1.90f};

	String[] ball_tmsx_tmsx1_12_na = {"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
	float[] ball_tmsx_tmsx1_12_pv = {11.50f,11.50f,11.50f,11.50f,11.50f,11.50f,11.50f,11.50f,9.50f,11.50f,11.50f,11.50f};
	private ArrayList<HashMap<String, Object>> getData() {
		list.clear();
		
		if(playTypeId == 9601){//特码
			String tm = getPL("9601-tm");
			for(int i=0;i<ball_1_49.length;i++){
				if(ball_1_49[i] == true){
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("bet_num", ""+(i+1));
					map.put("bet_name", ""+(i+1));
					map.put("peilv", ""+tm);
					map.put("money", 10);
					list.add(map);
				}
			}
			for(int i=0;i<ball1_t1_t17.length;i++){
				if(ball1_t1_t17[i] == true){
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("bet_num", "t"+(i+1));
					map.put("bet_name", ball_tm_t1_t17_na[i]);
					map.put("peilv", ""+getPL("9601-t"+(i+1)));//ball_tm_t1_t17_pv[i]);
					map.put("money", 10);
					list.add(map);
				}
			}
		}else if(playTypeId == 9602){//正码
			String zm = getPL("9602-zm");
			for(int i=0;i<ball_1_49.length;i++){
				if(ball_1_49[i] == true){
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("bet_num", ""+(i+1));
					map.put("bet_name", ""+(i+1));
					map.put("peilv", ""+zm);//7.18);
					map.put("money", 10);
					list.add(map);
				}
			}
			for(int i=0;i<ball1_zm_t1_t4.length;i++){
				if(ball1_zm_t1_t4[i] == true){
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("bet_num", "h"+(i+1));
					map.put("bet_name", ball_zm_t1_t4_na[i]);
					map.put("peilv", ""+getPL("9602-h"+(i+1)));//1.90);
					map.put("money", 10);
					list.add(map);
				}
			}
		}else if(playTypeId == 9603){//正码te
			String zmt = getPL("9603-zmt");
			for(int i=0;i<ball_1_49.length;i++){
				if(ball_1_49[i] == true){
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("bet_num", ""+(i+1));
					map.put("bet_name", ""+(i+1));
					map.put("peilv", ""+zmt);//42.00);
					map.put("money", 10);
					list.add(map);
				}
			}
			for(int i=0;i<ball_zmt_zmt1_zmt17.length;i++){
				if(ball_zmt_zmt1_zmt17[i] == true){
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("bet_num", "zmt"+(i+1));
					map.put("bet_name", ball_zmt_zmt1_zmt17_na[i]);
					map.put("peilv", ""+getPL("9603-zmt"+(i+1)));//ball_zmt_zmt1_zmt17_pv[i]);
					map.put("money", 10);
					list.add(map);
				}
			}
		}else if(playTypeId == 9604){//特码生肖
			for(int i=0;i<ball_tmsx_tmsx1_12.length;i++){
				if(ball_tmsx_tmsx1_12[i] == true){
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("bet_num", "tmsx"+(i+1));
					map.put("bet_name", ball_tmsx_tmsx1_12_na[i]);
					map.put("peilv", ""+getPL("9604-tmsx"+(i+1)));//ball_tmsx_tmsx1_12_pv[i]);
					map.put("money", 10);
					list.add(map);
				}
			}
		}
		// 拿到新的号码放到List
		return list;
	}

	private String getPL(String string) {
		String res = "0.00";
		for(Map<String, Object>item:plList){
			if(item.get("Type").toString().equals(string)){
				DecimalFormat decimalFormat=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
				res = decimalFormat.format(Float.parseFloat(item.get("PL").toString()));
				return res;
			}
		}
		return res;
	}

	public int[] RandomGenData(int min, int max, int num, String color) {
		int[] temp;
		Random rndNumber;
		rndNumber = new Random();
		temp = new int[num];
		ArrayList<Integer> a1 = new ArrayList<Integer>();

		for (int i = min; i <= max; i++) {
			a1.add((Integer) i);
		}
		for (int i = 0; i < num; i++) {
			int index = rndNumber.nextInt(max - min + 1 - i);
			Object o = a1.get(index);
			temp[i] = ((Integer) o).intValue();
			a1.remove(index);
		}
		Arrays.sort(temp);
		return temp;
	}

	// 计算注数和金额
	public void FactoryAllResult() {
		// 计算注数和金额
		int money = 0;
		for (int i = 0; i < money_dintInt.getSize(); i++) {
			money = money + money_dintInt.getInt(i);
		}
		allMoney = money;
		buy_ssqxhlb_TextView03.setText(money * 1 * 1 + "");
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == REQ_CODE_BUYMAIN || requestCode == REQ_CODE_NO_MONEY_2_RECHARGE) {
				ApplicationData.getInstance().initList();
				setResult(RESULT_OK);
				finish();
			}
		}
	};

	// TitleBar标题栏
	public void titleBar() {
		Button back = (Button) findViewById(R.id.top_title_back);
		back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				onKeyDown(KeyEvent.KEYCODE_BACK, null);
			}
		});
		TextView info = (TextView) findViewById(R.id.top_title_info);
		info.setText("六合彩-"+strPlayType);// R.string.buy_ssq_lb 双色球-普通
		Button refres = (Button) findViewById(R.id.top_title_refres);
		refres.getBackground().setAlpha(255);
		refres.setVisibility(View.INVISIBLE);
		refres.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (ApplicationData.getInstance().getList_ssqdto().size() != 0) {
				MyDialog.Builder dialog = new MyDialog.Builder(context);
				dialog.setTitle("退出提示");
				dialog.setMessage("是否清除本次选号?");
				dialog.setPositiveButton("清除", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 获得自定义的应用程序BallDTO,初始化
						ApplicationData.getInstance().initList();
						dialog.cancel();
						finish();
					}
				});
				dialog.setNegativeButton("保存", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						int size = ApplicationData.getInstance().getList_ssqdto().size();
						pUtil.putInt("ssq_size", size);
						for (int i = 0; i < size; i++) {
							pUtil.putString("ssq_ball" + i, ApplicationData.getInstance().getList_ssqdto().get(i)
									.getSsq_ball());
						}
						pUtil.putInt("ssq_allMoney", ApplicationData.getInstance().getAll_ssqmoney());

						ApplicationData.getInstance().initList();
						dialog.cancel();
						finish();
						Toast.makeText(context, R.string.design_change_saved, Toast.LENGTH_SHORT).show();
					}
				}).create().show();
			} else {
				// 无对话框退出
				ApplicationData.getInstance().initList();
				finish();
			}
		} else {
			return super.onKeyDown(keyCode, event);
		}
		return true;
	}

	@Override
	public void finish() {
		super.finish();
		SysApplication.getInstance().removeActivity(this);
	};
	@Override  
	public boolean dispatchTouchEvent(MotionEvent ev) {  
	    if (ev.getAction() == MotionEvent.ACTION_DOWN) {  
	        View v = getCurrentFocus();  
	        if (isShouldHideInput(v, ev)) {  
	            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
	            if (imm != null) {  
	                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  
	            }  
	        }  
	        return super.dispatchTouchEvent(ev);  
	    }  
	    // 必不可少，否则所有的组件都不会有TouchEvent了  
	    if (getWindow().superDispatchTouchEvent(ev)) {  
	        return true;  
	    }  
	    return onTouchEvent(ev);  
	}
	public  boolean isShouldHideInput(View v, MotionEvent event) {  
	    if (v != null && (v instanceof EditText)) {  
	        int[] leftTop = { 0, 0 };  
	        //获取输入框当前的location位置  
	        v.getLocationInWindow(leftTop);  
	        int left = leftTop[0];  
	        int top = leftTop[1];  
	        int bottom = top + v.getHeight();  
	        int right = left + v.getWidth();  
	        if (event.getX() > left && event.getX() < right  
	                && event.getY() > top && event.getY() < bottom) {  
	            // 点击的是输入框区域，保留点击EditText的事件  
	            return false;  
	        } else {  
	            return true;  
	        }  
	    }  
	    return false;  
	} 
}
